package in.iqing.module.content;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import java.util.List;

import in.iqing.app.R;
import in.iqing.control.util.UnitUtil;
import in.iqing.model.bean.Page;
import in.iqing.model.sp.SettingSP;

/**
 * Created by neavo on 2014/9/8.
 */

public class ContentView extends View {
    private static final String TAG = "ContentView";

    private Page page;
    private Paint charPaint;

    private int x;
    private int lineNum;
    private int lineSpace;
    private int charHeight;

    public ContentView(Context ctx) {
        super(ctx);
    }

    public ContentView prepare(Page page, Paint charPaint, int lineNum, int lineSpace, int charHeight) {
        this.page = page;
        this.charPaint = charPaint;

        this.lineNum = lineNum;
        this.lineSpace = lineSpace;
        this.charHeight = charHeight;
        this.x = UnitUtil.toPx(getContext(), 8);

        return this;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setBackgroundResource(SettingSP.getSingleton(getContext()).getNightMode()
                ? R.drawable.content_night_bg
                : R.drawable.content_day_bg);

        if (lineNum > 0 && page != null) {
            int y = charHeight;
            List<String> lines = page.getLines();

            for (int i = 0; i < lineNum; i++) {
                if (i == lines.size()) {
                    break;
                }

                if (i > 0) {
                    y = y + charHeight + lineSpace;
                }

                canvas.drawText(lines.get(i), x, y, charPaint);
            }
        }
    }
}
